<?php
  require_once('db.php');

  if(isset($_POST['email']) && !empty($_POST['email'])) {

    unset($_POST['cpassword']);
    unset($_POST['submitUsers']);
//pr($_POST);
    $q = '';
    foreach($_POST as $colName=>$colValue) {
      pr("`$colName`='$colValue', ");
      $q .= "`$colName`='$colValue', ";

    }
//pr(" `added_date`=NOW(),`status`='1'");
    $q .= " `added_date`=NOW(),`status`='1'";

    $insertQuery = " INSERT INTO `users` SET  $q";
//pr($insertQuery);
    mysql_query($insertQuery) or die(mysql_error());
    if(mysql_affected_rows() > 0) {
      echo "<h2>USer Inserted successfully</h2>";
    }
  }
  ###Select Users from Database####3
  $selQuery = "SELECT * FROM `users`";
  $selResult = mysql_query($selQuery)or die(mysql_error());
//var_dump($selResult);
  ##############################
  ## GET TOTAL NUMBER OF USERS##
  ///$selCountQuery = "SELECT COUNT(*)  FROM `users`";
  $selCountQuery = "SELECT COUNT(*) as `total` FROM `users`";
  $selCountResult = mysql_query($selCountQuery)or die(mysql_error());
  $selCountAns = mysql_fetch_assoc($selCountResult);
  //pr($selCountAns);
  $totalUsers = $selCountAns['total'];
?>

<form name="users" method="post" action="">
  <table border="1" width="80%" align="center">
    <thead>
      <tr><th colspan="2">Please fill the details</th></tr>
    </thead>

    <tbody>
      <tr>
        <td>Name :</td>
        <td><input type="text" required name="uname"></td>
      </tr>

      <tr>
        <td>Email :</td>
        <td><input type="email" required name="email"></td>
      </tr>

      <tr>
        <td>Password :</td>
        <td><input type="password" autocomplete="off" required name="password"></td>
      </tr>

      <tr>
        <td>Confirm Password :</td>
        <td><input type="password" required name="cpassword"></td>
      </tr>

      <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="submitUsers" value="I am done!!"></td>
      </tr>

    </tbody>
  </table>
</form>

<hr>
Total Users :<strong><?=$totalUsers?></strong>
<hr>

<table border="1" cellpadding="5" cellspacing="0" align="center" width="80%">
  <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
      <th>Email</th>
      <th>Added Date</th>
      <th>Status</th>
    </tr>
  </thead>

  <tbody>
<?php
  while($user = mysql_fetch_assoc($selResult)) {
    #pr($user);
?>
    <tr>
      <td><?=$user['id']?></td>
      <td><?=$user['uname']?></td>
      <td><a href="mailto:<?=$user['email']?>"><?=$user['email']?></a></td>
      <td><?=date('d-M-Y g:i:s a',strtotime($user['added_date']))?></td>
      <td><?=$user['status']?></td>
    </tr>
<?php
  }//end while
?>

  </tbody>

</table>




